package com.venutech.gongan.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.venutech.gongan.dao.TbWorkMapper;
import com.venutech.gongan.dao.TbWorkOrderMapper;
import com.venutech.gongan.entity.TbWork;
import com.venutech.gongan.entity.TbWorkOrder;
import com.venutech.gongan.service.ITbWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 巡检Service业务层处理
 * 
 * @author linxb
 * @date 2023-09-18
 */
@Service
public class TbWorkOrderServiceImpl implements ITbWorkOrderService
{
    @Autowired
    private TbWorkOrderMapper tbWorkOrderMapper;
    @Autowired
    private TbWorkMapper workMapper;

    /**
     * 查询巡检
     * 
     * @param wkId 巡检主键
     * @return 巡检
     */
    @Override
    public TbWorkOrder selectTbWorkOrderByWkId(Long wkId)
    {
        return tbWorkOrderMapper.selectTbWorkOrderByWkId(wkId);
    }

    /**
     * 查询巡检列表
     * 
     * @param TbWorkOrder 巡检
     * @return 巡检
     */
    @Override
    public List<TbWorkOrder> selectTbWorkOrderList(TbWorkOrder tbWorkOrder)
    {
        return tbWorkOrderMapper.selectTbWorkOrderList(tbWorkOrder);
    }

    /**
     * 新增巡检
     * 
     * @param TbWorkOrder 巡检
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbWorkOrder(TbWorkOrder tbWorkOrder)
    {
        tbWorkOrderMapper.insertTbWorkOrder(tbWorkOrder);
//        TbOpWorkOrder opWorkOrder=tbOpWorkOrderMapper.selectTbOpWorkOrderLast(tbOpWorkOrder);
        TbWork work=new TbWork();
        work.setWkSqUserId(tbWorkOrder.getWkSqUserId());
        work.setFaultType(tbWorkOrder.getFaultType());
        work.setWkSqTime(tbWorkOrder.getWkSqTime());
        work.setWkSpUserId(tbWorkOrder.getWkSpUserId());
        work.setWkContent(tbWorkOrder.getWkContent());
        work.setWkState((long)0);
        work.setWkOrderId(tbWorkOrder.getWkId());
        work.setWkOrderType(1);//巡检工单
        work.setImageDiagnosis(tbWorkOrder.getImageDiagnosis());
        work.setDataException(tbWorkOrder.getDataException());
        work.setNetworkStatus(tbWorkOrder.getNetworkStatus());
        work.setVideoFaultDetection(tbWorkOrder.getVideoFaultDetection());
        return workMapper.insertTbWork(work);
    }

    /**
     * 修改巡检
     * 
     * @param TbWorkOrder 巡检
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTbWorkOrder(TbWorkOrder tbWorkOrder)
    {
        tbWorkOrderMapper.updateTbWorkOrder(tbWorkOrder);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        tbWorkOrder.setWkOrderId(tbWorkOrder.getWkId());
        TbWork tbWork=workMapper.selectWorkByTbWorkOrder(tbWorkOrder);
        TbWork work=new TbWork();
        work.setWkId(tbWork.getWkId());
        work.setFaultType(tbWorkOrder.getFaultType());
        work.setWkSqUserId(tbWorkOrder.getWkSqUserId());
        work.setWkSqTime(tbWorkOrder.getWkSqTime());
        work.setWkSpUserId(tbWorkOrder.getWkSpUserId());
        work.setWkContent(tbWorkOrder.getWkContent());
        work.setWkState((long)0);
        work.setWkOrderId(tbWorkOrder.getWkId());
        work.setWkOrderType(1);//巡检工单
        work.setImageDiagnosis(tbWorkOrder.getImageDiagnosis());
        work.setDataException(tbWorkOrder.getDataException());
        work.setNetworkStatus(tbWorkOrder.getNetworkStatus());
        work.setVideoFaultDetection(tbWorkOrder.getVideoFaultDetection());
        return workMapper.updateTbWork(work);
    }

    /**
     * 批量删除巡检
     * 
     * @param wkIds 需要删除的巡检主键
     * @return 结果
     */
    @Override
    public int deleteTbWorkOrderByWkIds(String wkIds)
    {
        return tbWorkOrderMapper.deleteTbWorkOrderByWkIds(Convert.toStrArray(wkIds));
    }

    /**
     * 删除巡检信息
     * 
     * @param wkId 巡检主键
     * @return 结果
     */
    @Override
    public int deleteTbWorkOrderByWkId(Long wkId)
    {
        return tbWorkOrderMapper.deleteTbWorkOrderByWkId(wkId);
    }

    @Override
    @Transactional
    public int approveTbWorkOrder(TbWorkOrder tbWorkOrder) {
        tbWorkOrderMapper.updateTbWorkOrder(tbWorkOrder);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        tbWorkOrder.setWkOrderId(tbWorkOrder.getWkId());
//        TbWorkOrder workOrder=new TbWorkOrder();
//        workOrder.setWkId(tbWorkOrder.getWkId());
//        TbWork tbWork=workMapper.selectWorkByTbWorkOrder(workOrder);
        TbWork tbWork=workMapper.selectWorkByTbWorkOrder(tbWorkOrder);
        TbWork work=new TbWork();
        work.setWkState((long)tbWorkOrder.getWkState());
        work.setWkSpTime(date);
        work.setWkId(tbWork.getWkId());
        return workMapper.updateTbWork(work);
    }

    @Override
    public int approveFaultOrder(TbWorkOrder tbWorkOrder) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        TbWork work=new TbWork();
        work.setWkState((long)tbWorkOrder.getWkState());
        work.setWkSpTime(date);
        work.setWkId(tbWorkOrder.getWkId());
        work.setWkSpUserId(tbWorkOrder.getWkSpUserId());
        return workMapper.updateTbWork(work);
    }
}
